package com.cjk.foundation.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.cjk.foundation.entity.MenuEntity;
import com.cjk.foundation.enums.IsStatus;

@Repository
public interface MenuDao {
	
	@ResultMap("resultMap")
	@Select("select a.* from menu a inner join menu_auth_role b on a.id = b.menu_id where a.parent_id = #{parentId} and b.role_id = #{roleId} and b.status = #{status}")
	public List<MenuEntity> selectMenuListByParentId(@Param("parentId") Integer parentId, @Param("roleId") Integer roleId, @Param("status") IsStatus status);
	
	@Insert("insert into menu_auth_role(menu_id, role_id, status) values(#{menuId}, #{roleId}, #{status})")
	public void insertMenuAuth(@Param("menuId") Integer menuId, @Param("roleId") Integer roleId, @Param("status") IsStatus status);
	
	@Update("update menu_auth_role set status = #{status} where menu_id = #{menuId} and role_id = #{roleId}")
	public void updateMenuAuth(@Param("menuId") Integer menuId, @Param("roleId") Integer roleId, @Param("status") IsStatus status);
	
	@Delete("delete from menu_auth_role where role_id = #{roleId}")
	public void deleteMenuAuthByRoleId(@Param("roleId") Integer roleId);
	
	//用于zTree组件
	@Select("select id, parent_id pId, title name from menu")
	public List<MenuEntity> selectZTree();
	
	@Select("select a.id, parent_id pId, title name, cast(case when b.status = 'TRUE' then 1 else 0 end as tinyint) checked from menu a left join menu_auth_role b on a.id = b.menu_id where b.role_id = #{roleId}")
	public List<MenuEntity> selectZTreeByRoleId(@Param("roleId") Integer roleId);
}
